<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of ObjectRelationalMapping
 *
 * @author Leo
 */

namespace bdd;

class ObjectRelationalMapping {

    //put your code here
    // @todo mettre en private
    public $_mapping = array();
    public $_table;

    protected function __construct($table) {
        $this->_table = $table;

        $array = \api\arrayFromIni($this->_table, __NAMESPACE__);
        foreach ($array as $name => $value) {
            $this->$name = $value;
        }
    }

    public final function insert() {

        $comma = false;
        $columns = '';
        $values = '';
        $arrayValues = array();

        foreach ($this->_mapping as $name => $value) {
            if ($comma) {
                $columns .= ',';
                $values .= ',';
            } else {
                $comma = true;
            }
            $columns .= '`' . $name . '`';
            $values .= ':'.$name;
            $arrayValues[$name] = $value;
        }

        $query = "INSERT INTO `persos` ($columns) VALUES ($values);";

        echo $query;
        print_r($arrayValues);
        $conn = Connection::getConnection();

        $stat = $conn->prepareQuery($query);

        $stat->execute($arrayValues);

        //echo $conn->getLink()->exec($query);
        //$stat = $conn->ExecQuery($query);
    }

    /* public final function update() {

      }

      public final function select() {

      }

      public final function delete() {

      } */

    public final function __get($name) {
        if (array_key_exists($name, $this->_mapping)) {
            return $this->_mapping[$name];
        }
    }

    public final function __set($name, $value) {
        $this->_mapping[$name] = $value;
    }

}

?>
